package com.itboye.textileexpres.util;

import com.itboye.textileexpres.base.MainApplication;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class DensityUtil {

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(float dpValue) {
		// 布局预览需要，try{}catch
		try {
			final float scale = MainApplication.context.getResources()
					.getDisplayMetrics().density;
			return (int) (dpValue * scale + 0.5f);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (int) dpValue;
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(float pxValue) {
		final float scale = MainApplication.context.getResources()
				.getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public static int screenHeigh() {

		WindowManager wm = (WindowManager) MainApplication.context
				.getSystemService(Context.WINDOW_SERVICE);

		return wm.getDefaultDisplay().getHeight();
	}

	public static int screenWidth() {

		WindowManager wm = (WindowManager) MainApplication.context
				.getSystemService(Context.WINDOW_SERVICE);

		return wm.getDefaultDisplay().getWidth();
	}
}